﻿namespace Index.TreeIndex
{
    public class TextDigits
    {
        private const int maxIndex = 7;

        public readonly int[] Digits;

        public int CurrentDigit
        {
            get { return this.Digits[this.Index]; }
        }

        public int Index { get; private set; }

        public bool IsLastDigit
        {
            get { return this.Index == maxIndex; }
        }

        public TextDigits(string text)
        {
            this.Digits = new int[text.Length * 4];

            int index = this.Digits.Length - 1;

            for (int k = text.Length - 1; k >= 0; k-- )
            {
                int mask = 0x0000000f;
                int value = (int) text[k];

                for(int n = 0; n < 4; n++)
                {
                    int digit = (value & mask) >> (n*4);
                    this.Digits[index--] = digit;
                    mask = mask << 4;
                }
            }

            this.Index = 0;
        }

        public bool NextDigit()
        {
            if(this.Index < maxIndex)
            {
                this.Index++;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}